package org.saltlux.study.jackrabbit;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SecondHopTester {
	Repository repository;
	Session session;

	@Before
	public void before() throws Exception {
		repository = RepositoryManager.getInstance().getRepository();
		session = repository.login(new SimpleCredentials("username", "password"
				.toCharArray()));
	}

	@After
	public void after() throws Exception {
		session.logout();
	}

	@Test
	public void testLogin() throws Exception {
		String user = session.getUserID();
		String name = repository.getDescriptor(Repository.REP_NAME_DESC);
		Assert.assertEquals("username", user);
		Assert.assertEquals("Jackrabbit", name);
	}

	@Test
	public void testInsertingData() throws Exception {
		Node root = session.getRootNode();

		// Store content
		Node hello = root.addNode("hello");
		Node world = hello.addNode("world");
		world.setProperty("message", "Hello, World!");
		session.save();

		// Retrieve content
		Node node = root.getNode("hello/world");
		Assert.assertEquals("/hello/world", node.getPath());
		Assert.assertEquals("Hello, World!", node.getProperty("message")
				.getString());

		// Remove content
		root.getNode("hello").remove();
		session.save();
	}
}
